
import javax.swing.*;
import java.io.*;
import java.util.*;

/**
 * Created by Scott Tiger.
 * User: tomaszsocha
 * Date: 2010-03-29
 * Time: 14:08:38
 */
public class ItemCraft {
    public static HashMap<String,Object> laczenia= new HashMap<String,Object>();
    public static String filtrpref;
    public static String filtrsuf;
    public static boolean jest;
    public static int deep;
    public static HashMap <String,Item> zbior = new HashMap<String,Item>();
    public static HashMap <String,Item> zbiorB = new HashMap<String,Item>();
    public static HashMap <String,Object> zbiorC = new HashMap<String,Object>();
    public static Collection<Item> fajne = new LinkedHashSet();
    public static HashMap <String,Integer> wagi = new HashMap<String,Integer>();
    public static Collection<Koszulka> zbrojownia= new LinkedList();
    public static long couter;

    public static void main(String[] args) {
        String zbrojownia_name  ="/Users/tomaszsocha/Desktop/bw/";
        String f_pre_name ="/Users/tomaszsocha/Desktop/bw/";
        String f_suf_name ="/Users/tomaszsocha/Desktop/bw/";
        if (args.length >0 ) {
           zbrojownia_name  +=args[0];
        } else {
            zbrojownia_name  +="itemy.csv";
        }
        if (args.length > 1) {
            f_pre_name  +=args[1];
        } else {
            f_pre_name  +="filtrPref.csv";
        }
        if (args.length > 2) {
            f_suf_name  +=args[2];
        } else {
            f_suf_name  +="filtrSuf.csv";
        }

        File zkFile = new File(zbrojownia_name);
        try {
            zbrojownia = initZbrojownia(zkFile,zbrojownia);
            deep = 0;//zbrojownia.size();
        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        //HashMap<String,Object> laczenia= new HashMap<String,Object>();
        File prefixyFile = new File("/Users/tomaszsocha/Desktop/bw/itme.csv");
        File sufixyFile = new File("/Users/tomaszsocha/Desktop/bw/sufy.csv");
        File filtrPFile = new File(f_pre_name);
        File filtrSFile = new File(f_suf_name);
        File wagaP = new      File("/Users/tomaszsocha/Desktop/bw/wagaPref.csv");
        File wagaS = new File("/Users/tomaszsocha/Desktop/bw/wagaSuf.csv");


        try {

         filtrpref = initfiltr(filtrPFile);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
      /*
        Iterator it ;
         Koszulka k ;
         Koszulka k2 ;
        String key;// = it.next().toString();


        it = zbrojownia.iterator();
          */
        System.out.println("deep: "+(zbrojownia.size() - deep));
        System.out.println("zbrojownia: "+zbrojownia.size());

        polaczItem(zbrojownia);
       // polaczKoszulke(zbrojownia);

        Iterator faj  = fajne.iterator();
        while (faj.hasNext()){
            Item key = (Item)faj.next();
            System.out.println(key.getName());
            System.out.println(key.getIngredients());        
        }

    }

    private static void initWagi(File wagaP, File wagaS) throws FileNotFoundException {
       Scanner scanner = new Scanner(wagaP,"UTF-8");
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String name = line.substring(0, line.indexOf(","));
              int waga = (int) Integer.parseInt(line.substring(line.indexOf(",")+1));
              wagi.put(name,waga);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
        scanner = new Scanner(wagaS,"UTF-8");
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              String line = scanner.nextLine() ;
              String name = line.substring(0, line.indexOf(","));
              int waga = (int) Integer.parseInt(line.substring(line.indexOf(",")+1));
              wagi.put(name,waga);
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
    }


    public static final HashMap processLineByLine(File fFile,HashMap laczenia) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
    try {
      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          String result = line.substring(0, line.indexOf(","));
          String param2 = line.substring(line.indexOf(",")+1,line.indexOf(",",line.indexOf(",")+1));
          String param1 = line.substring(line.indexOf(",",line.indexOf(",")+1)+1);
          HashMap<String,String> o = new HashMap<String,String>();
          if (laczenia.containsKey(param1)) {
          o = (HashMap<String,String>) laczenia.get(param1);}
          o.put(param2,result);
          laczenia.put(param1,o);
      }
    }
    finally {
      //ensure the underlying stream is always closed
      scanner.close();
    }
        return laczenia;
  }




    public static HashMap initLaczenia(HashMap laczenia){
        String param1 = "";
        String param2 = "";
        String result = "";
        HashMap<String,String> o = new HashMap<String,String>();
        if (laczenia.containsKey("")) {
        o = (HashMap<String,String>) laczenia.get(param1);}
        o.put(param2,result);
        laczenia.put(param1,o);
        return o;

    };





    public static Collection initZbrojownia(File fFile,Collection z) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
    try {
       Objeto ob = new Objeto("/Users/tomaszsocha/Desktop/bw/wagiBronie.csv","/Users/tomaszsocha/Desktop/bw/bronie.csv");

      //first use a Scanner to get each line
      while ( scanner.hasNextLine() ){
          String line = scanner.nextLine() ;
          if (line.indexOf(",")==-1){
            z.add( new Item(line,ob));
          }   else
          {
          String pref = line.substring(0, line.indexOf(","));
          String suf =line.substring(line.indexOf(",")+1);
          z.add(new Koszulka(pref,suf));
          }    
      }

      Iterator it = z.iterator();
        while (it.hasNext()){
          Object o  =  it.next();
            if (o instanceof Item) {
            System.out.println(((Item) o).getIngredients());
            } else  {
            System.out.println(((Koszulka) o).getIngredients());
            }
        }
    }
    finally {
      //ensure the underlying stream is always closed
      scanner.close();
    }


        return z;
    }

    public static String initfiltr(File fFile) throws FileNotFoundException {
    Scanner scanner = new Scanner(fFile,"UTF-8");
      String line = "";
        try {
          //first use a Scanner to get each line
          while ( scanner.hasNextLine() ){
              if (line.equals("")){
                  line = scanner.nextLine() ;
              }   else {
                  line += "|"+scanner.nextLine();
              }
          }
        }
        finally {
          //ensure the underlying stream is always closed
          scanner.close();
        }
      return line;
    }



    public void polacz(HashMap laczenia,Collection zbrojownia){
        Koszulka k1;
        Koszulka k2;

    }


    public Collection<Koszulka> getZbrojownia(){
        return zbrojownia;
    }

    public static String polacz(String pref1,String pref2){
       return (String) ((HashMap) laczenia.get(pref1)).get(pref2);

    }

    public static Koszulka polaczKoszulki(Koszulka k1,Koszulka k2){
         k1.getPrefix();
         k2.getPrefix();
//        System.out.println(k1.getPrefix()+"  "+k1.getSufix()+" + "+k2.getPrefix()+"  "+k2.getSufix() + "polacz");
         couter ++;
         Koszulka wynik = new  Koszulka(polacz(k1.getPrefix(),k2.getPrefix()),polacz(k1.getSufix(),k2.getSufix()),k1,k2,wagi);
         return wynik;
    }


    public static void polaczKoszulke(Collection zk,Koszulka k ) {
        if (zk.size() > 0) {
        for (int i =0;i<zk.size();i++ ) {
        Koszulka shirt = (Koszulka) zk.toArray()[i];
        zk.remove(shirt);
        Koszulka newShirt = polaczKoszulki(k,shirt);

            if (((newShirt.getPrefix().matches(filtrpref) ) && (newShirt.getSufix().matches(filtrsuf)) || (newShirt.getSufix().matches(filtrsuf) )))
            {
                System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());
                jest = true;
            }
        if (zk.size()>0) {
            polaczKoszulke(zk,newShirt);
        }

        zk.add(shirt);


            }
        }
    }

        public static void polaczKoszulke(Collection zk) {
        if (zk.size() > deep ) {
            for (int i =0;i<zk.size()-1;i++ ) {
            Koszulka shirt = (Koszulka) zk.toArray()[i];
            if (zk.size()>22) {
            Calendar calendar = new GregorianCalendar();
                String am_pm;
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                if(calendar.get(Calendar.AM_PM) == 0)
                  am_pm = "AM";
                else
                  am_pm = "PM";
              // System.out.println("Current Time : " + hour + ":"
              //  + minute + ":" + second + " " + am_pm+ " size :"+zk.size()+" -- "+shirt.getPrefix()+"  "+shirt.getSufix()) ;
            }
            zk.remove(shirt);
                for (int j =0;j<zk.size();j++ ) {
                Koszulka k = (Koszulka) zk.toArray()[j];
                zk.remove(k);
                Koszulka newShirt = polaczKoszulki(k,shirt);
                  if (czyWarto(shirt,k,newShirt)) {
                   // System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());
                   // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());

                    if (((newShirt.getPrefix().matches(filtrpref) ) && (newShirt.getSufix().matches(filtrsuf)) ))//|| (newShirt.getSufix().matches(filtrsuf) )))
                    {
                       // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                        //System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix());

                        if (!zbior.containsKey(newShirt.getPrefix()+newShirt.getSufix()+k.getPrefix()+k.getSufix()+shirt.getPrefix()+shirt.getSufix())) {


                        zbior.put(newShirt.getPrefix()+newShirt.getSufix()+k.getPrefix()+k.getSufix()+shirt.getPrefix()+shirt.getSufix(),newShirt);
                        zbiorB.put(newShirt.getPrefix()+"."+newShirt.getSufix(),newShirt);
                        Collection<Koszulka> kList = new LinkedList();
                        if (zbiorC.containsKey(newShirt.getPrefix()+"."+newShirt.getSufix())){
                           kList = (LinkedList)zbiorC.get(newShirt.getPrefix()+"."+newShirt.getSufix());
                           kList.add(newShirt);
                        } else {
                           kList.add(newShirt);
                           System.out.println(newShirt.getPrefix()+"  "+newShirt.getSufix()+" cost :" + newShirt.totalCost());
                           System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                           System.out.println(newShirt.getIngredients());
                           time();
                        }

                        zbiorC.put(newShirt.getPrefix()+"."+newShirt.getSufix(),kList);
                        fajne.add(newShirt);
                        jest = true;

                        }
                    }

                    zk.add(newShirt);
                   if (zk.size()>1)
                    polaczKoszulke(zk);
                    zk.remove(newShirt);
                  }
                zk.add(k);


                 }
            zk.add(shirt);

            }
        }
    }




    public static void polaczItem(Collection zk) {
        if (zk.size() > deep ) {
            for (int i =0;i<zk.size()-1;i++ ) {
            Item item = (Item) zk.toArray()[i];
            if (zk.size()>22) {
            Calendar calendar = new GregorianCalendar();
                String am_pm;
                int hour = calendar.get(Calendar.HOUR);
                int minute = calendar.get(Calendar.MINUTE);
                int second = calendar.get(Calendar.SECOND);
                if(calendar.get(Calendar.AM_PM) == 0)
                  am_pm = "AM";
                else
                  am_pm = "PM";
              // System.out.println("Current Time : " + hour + ":"
              //  + minute + ":" + second + " " + am_pm+ " size :"+zk.size()+" -- "+shirt.getPrefix()+"  "+shirt.getSufix()) ;
            }
            zk.remove(item);
            Objeto o;
                try {
                    o = new Objeto("/Users/tomaszsocha/Desktop/bw/wagiBronie.csv","/Users/tomaszsocha/Desktop/bw/bronie.csv");

                for (int j =0;j<zk.size();j++ ) {
                Item k = (Item) zk.toArray()[j];
                zk.remove(k);
                Item newItem = o.polaczItemy(k,item);
                  if (czyWarto(item,k,newItem)) {
                   // System.out.println(newItem.getPrefix()+"  "+newItem.getSufix());
                   // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());

                    if (((newItem.getName().matches("Skorpion|Mp5k|Desert Eagle") ) ))//|| (newItem.getSufix().matches(filtrsuf) )))
                    {
                       // System.out.println(k.getPrefix()+"  "+k.getSufix()+" + "+shirt.getPrefix()+"  "+shirt.getSufix());
                        //System.out.println(newItem.getPrefix()+"  "+newItem.getSufix());

                        if (!zbior.containsKey(newItem.getName()+"="+k.getName()+"+"+item.getName())) {


                        zbior.put(newItem.getName()+"="+k.getName()+"+"+item.getName(),newItem);
                        zbiorB.put(newItem.getPrefix()+"."+newItem.getSufix(),newItem);
                        Collection<Item> kList = new LinkedList();
                        if (zbiorC.containsKey(newItem.getPrefix()+"."+newItem.getSufix())){
                           kList = (LinkedList)zbiorC.get(newItem.getPrefix()+"."+newItem.getSufix());
                           kList.add(newItem);
                        } else {
                           kList.add(newItem);
                           System.out.println(newItem.getName()+" cost :" + newItem.totalCost());
                           System.out.println(k.getName()+" + "+item.getName());
                           System.out.println(newItem.getIngredients());
                           time();
                        }

                        zbiorC.put(newItem.getPrefix()+"."+newItem.getSufix(),kList);
                        fajne.add(newItem);
                        jest = true;

                        }
                    }

                    zk.add(newItem);
                   if (zk.size()>1)
                    polaczItem(zk);
                    zk.remove(newItem);
                  }
                zk.add(k);


                 }
            } catch (FileNotFoundException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
            zk.add(item);

            }
        }
    }

    private static boolean czyWarto(Item item, Item k, Item newItem) {
            if (item.getWaga()==newItem.getWaga() || k.getWaga() == newItem.getWaga() ) {
                return false;
            }
        return true;
    }

    private static void time() {
        Calendar calendar = new GregorianCalendar();
            String am_pm;
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);
            int second = calendar.get(Calendar.SECOND);
            if(calendar.get(Calendar.AM_PM) == 0)
              am_pm = "AM";
            else
              am_pm = "PM";
           System.out.println("Current Time : " + hour + ":"
            + minute + ":" + second + " " + am_pm) ;
   }


    private static boolean czyWarto(Koszulka shirt, Koszulka k, Koszulka newShirt) {

            if (shirt.getWagaPref()==newShirt.getWagaPref() && shirt.getWagaSuf() >= newShirt.getWagaSuf() ) {
                return false;
            } else if (k.getWagaPref()==newShirt.getWagaPref() && k.getWagaSuf() >= newShirt.getWagaSuf() ) {
                return false;
            } else if (shirt.getWagaPref()>=newShirt.getWagaPref() && shirt.getWagaSuf() == newShirt.getWagaSuf() ) {
                return false;
            } else if (k.getWagaPref()>=newShirt.getWagaPref() && k.getWagaSuf() == newShirt.getWagaSuf() ) {
                return false;

            } else

            { return (!(newShirt.czySkladnik(newShirt))); }


    }

}

